const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videos: [],
    coachDetail: {},
    courseList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    const url = app.globalData.url;
    const baseUrl = app.globalData.baseUrl;
    const openId = wx.getStorageSync("openId");
    wx.request({
      url: url+'/coach/' + openId,
      method: 'GET',
      success: function(response) {
        if (response.data.success) {
          const content = response.data.content;
          wx.setNavigationBarTitle({
            title: content.name
          });
          const videos = [];
          if (content.videos && content.videos.length>0){
            content.videos.forEach(function(item){
              if (videos.length<2){
                videos.push(baseUrl + '/file/' + item.picName);
              }
            });
          }
          that.setData({
            coachDetail: content,
            videos
          });
        } else {
          wx.showToast({
            title: '获取教练信息出错',
            icon: 'none'
          })
        }
      }
    })
  },


  toVideo: function() {
    const coachDetail = this.data.coachDetail;
    wx.setStorageSync("videos", JSON.stringify(coachDetail.videos));
    wx.navigateTo({
      url: '/pages/video/video',
    })
  },
  appointment: function() {
    const coach = this.data.coachDetail;
    wx.setStorageSync("coach", JSON.stringify(coach));
    //检查是否是会员
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    wx.request({
      url: url + '/appointment/checkIsMember/' + openId,
      success: function(response) {
        if (response.data.success) {
          wx.navigateTo({
            url: '/pages/memberAppointment/memberAppointment',
          })
        }else{
          wx.navigateTo({
            url: '/pages/appointment/appointment',
          })
        }
      }
    })
    
  }
})